package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.voicechat.firstmet.App;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.bean.PhotoAlbumPictureBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.view.CircleImageView;
import com.voicechat.firstmet.view.grouprecyclerview.BaseViewHolder;
import com.voicechat.firstmet.view.grouprecyclerview.GroupedRecyclerViewAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: PhotoAlbumGroupAdapter
 * @Description: 相册页面的adapter
 * @Author: ljp
 * @CreateDate: 2018/10/30 9:42
 * @Version: 1.0
 */
public class PhotoAlbumGroupAdapter extends GroupedRecyclerViewAdapter {

    private List<PhotoAlbumPictureBean.PageBean.SetBean> setBeanList;
    private Context context;
    private String TAG = "PhotoAlbumGroupAdapter";
    private OnClick onClick;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                //String tag = data.getString("tag");
                CircleImageView view = (CircleImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(App.getContext(), bitmap, view);
                /*GlideApp.with(context)
                        .load(bitmap)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(view);*/
            }
        }
    };
    private List<String> sb;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public PhotoAlbumGroupAdapter(Context context, List<PhotoAlbumPictureBean.PageBean.SetBean> setBeanList) {
        super(context);
        this.context = context;
        this.setBeanList = setBeanList;
        sb = new ArrayList<>();
    }
    @Override
    public int getGroupCount() {
        Log.i(TAG, "getGroupCount: --------" + setBeanList.size());
        return setBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        //Log.i(TAG, "getChildrenCount: ------------"+setBeanList.get(groupPosition).getValie().size());
        Log.i(TAG, "getChildrenCount: --------" + setBeanList.get(groupPosition).getValie().size());
        return setBeanList.get(groupPosition).getValie().size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }
    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_group_photo_album;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_photo_album;
    }

    private String lastTime;

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        PhotoAlbumPictureBean.PageBean.SetBean listBean = setBeanList.get(groupPosition);
        //if(holder instanceof HeadViewHolder){
        // HeadViewHolder headViewHolder = (HeadViewHolder) holder;
       /* if(!TextUtils.isEmpty(lastTime) && lastTime.equals(setBeanList.get(groupPosition).getTime())){
            holder.get(R.id.tv_photo_time).setVisibility(View.GONE);
        }else {
            holder.get(R.id.tv_photo_time).setVisibility(View.VISIBLE);*/
        holder.setText(R.id.tv_photo_time, listBean.getTime());
        //  }
        // headViewHolder.tvPhotoTime.setText(setBean.getTime());
        // }
        lastTime = setBeanList.get(groupPosition).getTime();
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        //String[] strings = Utils.spitPictureUrl(setBeanList.get(groupPosition).getValie().getAlbumUrl());
        PhotoAlbumPictureBean.PageBean.SetBean.ValieBean valieBean = setBeanList.get(groupPosition).getValie().get(childPosition);
        if(!TextUtils.isEmpty(valieBean.getAlbumUrl())) {
            if (!valieBean.getAlbumUrl().equals(holder.get(R.id.circle_picture).getTag(R.id.image_icon))) {
                // 加载图片
                holder.get(R.id.circle_picture).setTag(R.id.image_icon, valieBean.getAlbumUrl());
                GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + valieBean.getAlbumUrl(),
                        holder.get(R.id.circle_picture));
                // setImage(string, holder.get(R.id.circle_picture));
                if(valieBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                    holder.get(R.id.image_un_select).setVisibility(View.GONE);
                }else if(valieBean.isSelectDelete() == -1){
                    holder.get(R.id.image_un_select).setVisibility(View.VISIBLE);
                    ((ImageView) holder.get(R.id.image_un_select)).setImageResource(R.mipmap.photo_un_select);
                }else {
                    holder.get(R.id.image_un_select).setVisibility(View.VISIBLE);
                    ((ImageView) holder.get(R.id.image_un_select)).setImageResource(R.mipmap.photo_select);
                }

            }else {
                if(valieBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                    holder.get(R.id.image_un_select).setVisibility(View.GONE);
                }else if(valieBean.isSelectDelete() == -1){
                    holder.get(R.id.image_un_select).setVisibility(View.VISIBLE);
                    ((ImageView) holder.get(R.id.image_un_select)).setImageResource(R.mipmap.photo_un_select);
                }else {
                    holder.get(R.id.image_un_select).setVisibility(View.VISIBLE);
                    ((ImageView) holder.get(R.id.image_un_select)).setImageResource(R.mipmap.photo_select);
                }
            }

        }
        holder.get(R.id.circle_picture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<PhotoAlbumPictureBean.PageBean.SetBean.ValieBean> valie = setBeanList.get(groupPosition).getValie();
                PhotoAlbumPictureBean.PageBean.SetBean.ValieBean valieBean1 = valie.get(childPosition);
                if(valieBean1.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                    holder.get(R.id.image_un_select).setVisibility(View.GONE);
                    onClick.onSetPictureClick(holder.get(R.id.circle_picture), valie, childPosition);
                }else if(valieBean1.isSelectDelete() == -1){
                    holder.get(R.id.image_un_select).setVisibility(View.VISIBLE);
                    ((ImageView) holder.get(R.id.image_un_select)).setImageResource(R.mipmap.photo_select);
                    valieBean1.setSelectDelete(1);
                    sb.add(valieBean1.getAlbumId());
                }else {
                    holder.get(R.id.image_un_select).setVisibility(View.VISIBLE);
                    ((ImageView) holder.get(R.id.image_un_select)).setImageResource(R.mipmap.photo_un_select);
                    valieBean1.setSelectDelete(-1);
                    if(sb.contains(valieBean1.getAlbumId())){
                        sb.remove(valieBean1.getAlbumId());
                    }
                }
                //notifyItemChanged(childPosition);
               // final List<PhotoAlbumPictureBean.PageBean.SetBean.ValieBean> valie = setBeanList.get(groupPosition).getValie();
                //onClick.onSetPictureClick(holder.get(R.id.circle_picture), valie, childPosition);
            }

        });
    }
    public List<String> getSb() {
        return sb;
    }


    class HeadViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_photo_time)
        TextView tvPhotoTime;

        public HeadViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    class ChildViewHolder extends BaseViewHolder {
        @BindView(R.id.circle_picture)
        CircleImageView circlePicture;
        @BindView(R.id.image_un_select)
        ImageView imageUnSelect;
        public ChildViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClick {
        void onSetPictureClick(View view, List<PhotoAlbumPictureBean.PageBean.SetBean.ValieBean> valie, int position);
    }
}
